/*
    REDLisp - interpreter/compiler for new dialect of LISP called REDLISP
    Copyright (C) 2006  Rudolf Olah

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include "types.h"
#include "core.h"

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>

#ifdef TARGET_OS_WINDOWS
#include "getopt.h"
#else
#include <unistd.h>
#endif

#ifdef READLINE
#include <readline/readline.h>
#include <readline/history.h>
#endif

using namespace std;

int main(int argc, char **argv)
{
  bool loadFile = false;
  List *system_env = 0;

  /*
    cout << loadFile (getHomeDirectory ())->toString () << endl;
    cout << loadFile ("functions.dlisp")->toString () << endl;
  */
  if (!loadFile)
    cout << "REDLisp 0.53\n(C) Copyright 2007 - Rudolf Olah\nLicensed under the GNU GPL"
	 << endl;
  /*
  else
    loadFile (filename);
  */

  List* systemEnv = new List ();
  //atexit (killSystemEnvironment);
  // Enter REPL
  char *expr;
#if defined (TARGET_OS_WINDOWS) || !defined (READLINE) 
  char buffer[256];
#endif
  Object *object;
  while (1)
    {
#if defined (TARGET_OS_WINDOWS) || !defined (READLINE)
      cout << ">> ";
      expr = fgets (buffer, 256, stdin);
      // Remove the newline
      expr[strlen (expr)-1] = '\0';
#else
      expr = readline (">> ");
      if (*expr)
        add_history (expr);
#endif
      object = eval (parse (expr), systemEnv);
      cout << object->toString () << endl;
    }
  delete object;
  return 0;
}
